package com.lzy.cloud.search.client.service.impl;

import com.lzy.cloud.search.client.entity.EsQuery;
import com.lzy.cloud.search.client.service.FullTextQueriesService;
import com.lzy.cloud.search.client.util.EsClient;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.QueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * 全文搜索
 */
@Service
public class FullTextQueriesServiceImpl implements FullTextQueriesService {

    @Autowired
    private EsClient client;


    /**
     * 最简单的查询 匹配所有文档
     * @param esQuery
     * @return
     * @throws Exception
     */
    @Override
    public SearchResponse matchAllQuerys(EsQuery esQuery)  {
        QueryBuilder qb = matchAllQuery();

        SearchResponse response = client.getConnection().prepareSearch(esQuery.getIndices())//可以是多个index
                .setTypes(esQuery.getTypes())//可以是多个类型
                .setQuery(qb)    // Query 查询条件
                .get();

        return response;

    }

    /**
     * 用于执行全文查询的标准查询，包括模糊匹配和词组或邻近程度的查询
     *
     * @throws Exception
     */
    @Override
    public SearchResponse matchQuerys(EsQuery esQuery) {

        QueryBuilder qb = matchQuery(esQuery.getFieldName(), esQuery.getFieldText());

        SearchResponse response = client.getConnection().prepareSearch(esQuery.getIndices())
                .setTypes(esQuery.getTypes())
                .setQuery(qb).get();


        //        QueryBuilder qb = matchQuery(
//                "message",                  //field 字段
//                "Elasticsearch"   // text
//        );
//
//        SearchResponse response = client.getConnection().prepareSearch("twitter")//可以是多个index
//                .setTypes("tweet")//可以是多个类型
//                .setQuery(qb)    // Query 查询条件
//                .get();

        return response;

    }

    /**
     * 多个字段查询
     *
     * @return
     * @throws Exception
     */
    @Override
    public SearchResponse multiMatchQuerys(EsQuery esQuery)  {

        QueryBuilder qb = multiMatchQuery(esQuery.getFieldText(), esQuery.getFieldNames());

        SearchResponse response = client.getConnection().prepareSearch(esQuery.getIndices())
                .setTypes(esQuery.getTypes())
                .setQuery(qb)
                .get();

        //        QueryBuilder qb = multiMatchQuery(
//                "kimchy elasticsearch", // text
//                "user", "message"       //fields 多个字段
//        );
//
//        SearchResponse response = client.getConnection().prepareSearch("twitter")//可以是多个index
//                .setTypes("tweet")//可以是多个类型
//                .setQuery(qb)    // Query 查询条件
//                .get();
//
        return response;
    }

    @Override
    public SearchResponse commonTermsQuerys(EsQuery esQuery) {

        QueryBuilder qb = commonTermsQuery(esQuery.getFieldName(), esQuery.getFieldText());

        SearchResponse response = client.getConnection().prepareSearch(esQuery.getIndices())
                .setTypes(esQuery.getTypes())
                .setQuery(qb)
                .get();

//
//        QueryBuilder qb = commonTermsQuery(
//                "name",    //field 字段
//                "kimchy");  // value
//
//        SearchResponse response = client.getConnection().prepareSearch("twitter")//可以是多个index
//                .setTypes("tweet")//可以是多个类型
//                .setQuery(qb)    // Query 查询条件
//                .get();

        return response;
    }
}
